﻿using FreeSql;
using Microsoft.Extensions.Configuration;
using Publicuse.Entity.sqlsugar;
using Publicuse.Interface;
using Publicuse.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Service.FreeSql
{
    public class SqlServerFreeSqlService : BaseFreeSqlService, ISqlServerFreeSqlService
    {
        public SqlServerFreeSqlService(IFreeSql freeSql, IConfiguration configuration) : base(freeSql)
        {
            FreeSqlBuilder builder = new FreeSqlBuilder();
            var mysql = new CustomConnectionConfig();
            configuration.Bind("SqlServerConnectionString", mysql);
            builder.UseConnectionString(DataType.SqlServer, AesEncryptionUtil.AESDecrypt(mysql.MasterConnectionString, out var msg));
            builder.UseMonitorCommand(arge =>
            {
                Console.WriteLine(arge.CommandText);
            });
            builder.UseSlave(mysql.SlaveConnectionString.Select(o => o.ConnectionString).ToArray());
            builder.UseSlaveWeight(mysql.SlaveConnectionString.Select(o => o.HitRate).ToArray());
        }
    }
}
